<template>
    <div id="login">
        <div class="login-box">
            <h1>患者服务床旁交互管理系统</h1>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
                     class="demo-ruleForm login-form">
                <el-form-item label="账号" prop="name">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="psw">
                    <el-input type="password" v-model="ruleForm.psw"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import {mapState} from "vuex"

    export default {
        name: "Login",
        data() {
            // 用户名验证
            let validateName = (rule, value, callback) => {
                value = value.trim()
                if (value === '') {
                    callback(new Error('请输入用户名'));
                }
                    // else if (!this.reg.regId.test(value)) {
                    //     callback(new Error('用户名不符合规则'));
                // }
                else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };

            // 密码验证
            let validatePsw = (rule, value, callback) => {
                value = value.trim()
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };

            return {

                // 表单验证
                rules: {
                    name: [
                        {validator: validateName, trigger: 'blur'}
                    ],
                    psw: [
                        {validator: validatePsw, trigger: 'blur'}
                    ]
                },
                // 表单信息
                ruleForm: {
                    name: 'admin',
                    psw: '123456',
                },
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // 发送请求向后台验证用户名以及密码的正确与否

                        if (this.ruleForm.name === "admin" && this.ruleForm.psw === "123456") {
                            // 将数据存在vuex和local中
                            // 模拟异步数据
                            setTimeout(() => {
                                this.$store.dispatch("setUserA", this.ruleForm.name)
                            }, 1000)
                            this.tools.saveS("user", this.ruleForm.name)
                            // 跳转首页
                            this.$router.replace("/")
                        }
                    } else {
                        this.$message.warning("请正确填写信息")
                        return false;
                    }
                });
            },
        }
    }
</script>

<style scoped lang="less">html, body {
    width: 100%;
    height: 100%;
}

#login {
    @bg: #283443;

    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    background-color: @bg;
    display: flex;
    justify-content: center;
    align-items: center;

    .login-box {
        h1 {
            text-align: center;
            font-weight: 800;
            font-size: 36px;
            margin-bottom: 50px;
        }

        .login-form {
            width: 500px;
            background-color: #fff;
            padding: 20px 0;
            border-radius: 10px;

            /deep/ .el-form-item__label:before { /* required提示去除 */
                content: "";
            }
        }
    }


}

</style>